/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xalan.filesearch.viewmodel;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import java.util.*;
import javax.swing.event.TreeModelEvent;
import xalan.filesearch.model.Catalogue;
import xalan.filesearch.model.FileDesc;
import xalan.filesearch.service.CatalogueService;
/**
 *
 * @author Piotrek
 */
public class CatalogueTreeModel implements TreeModel {
    private Vector vector = new Vector();
    private CatalogueService service;

    public CatalogueTreeModel(CatalogueService service) {
        this.service = service;
    }

    public void addTreeModelListener( TreeModelListener listener ) {
      if ( listener != null && !vector.contains( listener ) ) {
         vector.addElement( listener );
      }
   }

   public void removeTreeModelListener( TreeModelListener listener ) {
      if ( listener != null ) {
         vector.removeElement( listener );
      }
   }

   public void fireTreeNodesChanged( TreeModelEvent e ) {
      Enumeration listeners = vector.elements();
      while ( listeners.hasMoreElements() ) {
         TreeModelListener listener = (TreeModelListener)listeners.nextElement();
         listener.treeNodesChanged( e );
      }
   }

   public void fireTreeNodesInserted( TreeModelEvent e ) {
      Enumeration listeners = vector.elements();
      while ( listeners.hasMoreElements() ) {
         TreeModelListener listener = (TreeModelListener)listeners.nextElement();
         listener.treeNodesInserted( e );
      }
   }

   public void fireTreeNodesRemoved( TreeModelEvent e ) {
      Enumeration listeners = vector.elements();
      while ( listeners.hasMoreElements() ) {
         TreeModelListener listener = (TreeModelListener)listeners.nextElement();
         listener.treeNodesRemoved( e );
      }
   }

   public void fireTreeStructureChanged( TreeModelEvent e ) {
      Enumeration listeners = vector.elements();
      while ( listeners.hasMoreElements() ) {
         TreeModelListener listener = (TreeModelListener)listeners.nextElement();
         listener.treeStructureChanged( e );
      }
   }

    // *** //
    public Object getRoot() {
        return service;
    }

    public Object getChild(Object parent, int index) {
        if(parent instanceof CatalogueService) {
            return ((CatalogueService)parent).getCatalogues().toArray()[index];
        }
        else if(parent instanceof Catalogue) {
            return ((Catalogue)parent).getFiles().toArray()[index];
        }
        else if(parent instanceof FileDesc) {
            return null;
        }
        
        return null;
    }

    public int getChildCount(Object parent) {
        if(parent instanceof CatalogueService) {
            return ((CatalogueService)parent).getCatalogues().size();
        }
        else if(parent instanceof Catalogue) {
            return ((Catalogue)parent).getFiles().size();
        }

        return 0;
    }

    public boolean isLeaf(Object node) {
        return node instanceof FileDesc;
    }

    public void valueForPathChanged(TreePath path, Object newValue) {
        // do nothing
    }

    public int getIndexOfChild(Object parent, Object child) {
        int i = 0;
        if(parent instanceof CatalogueService) {
            for(Catalogue c: ((CatalogueService)parent).getCatalogues()) {
                if(c.equals(child)) {
                    return i;
                }
                ++i;
            }
        }
        else if(parent instanceof Catalogue) {
            for(FileDesc f: ((Catalogue)parent).getFiles()) {
                if(f.equals(child)) {
                    return i;
                }
                ++i;
            }
        }

        return i;
    }
}
